package com.jinsp.webviewservice.webview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by luoshuai on 1/4/22
 * <p>
 * WebView进度条
 */
public class WebProgressView extends View {

    private final Paint mProgressPaint;
    private int offsetX;        //横坐标偏移量
    private boolean isStop;

    public WebProgressView(Context context) {
        this(context, null);
    }

    public WebProgressView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WebProgressView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mProgressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mProgressPaint.setColor(Color.RED);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);
        mProgressPaint.setStrokeWidth(10);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isStop) {
            //绘制进度条
            int maxProgress = (int) (getWidth() * 0.8);
            if (offsetX <= maxProgress) {
                canvas.drawLine(0, 0, offsetX, 0, mProgressPaint);
                offsetX += (getWidth() * 0.05);
                postInvalidateDelayed(52);
            } else {
                canvas.drawLine(0, 0, maxProgress, 0, mProgressPaint);
            }
        }
    }

    /**
     * 停止动画
     */
    public void stop() {
        isStop = true;
    }

    /**
     * 设置进度条颜色
     *
     * @param progressColor 颜色
     */
    public void setProgressColor(int progressColor) {
        mProgressPaint.setColor(progressColor);
    }
}
